package com.usian.wemedia.controller;


import com.usian.apis.wemedia.WmNewsControllerApi;
import com.usian.model.common.dtos.ResponseResult;

import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.mapper.WmUserMapper;
import com.usian.wemedia.service.WmNewsService;
import com.usian.wemedia.service.impl.WmNewsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {

    @Autowired
    private WmNewsServiceImpl wmNewsService;

    @Autowired
    private WmUserMapper wmUserMapper;

    @PostMapping("/list")
    @Override
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto wmNewsPageReqDto){
        return wmNewsService.findAll(wmNewsPageReqDto);
    }

    @PostMapping("/submit")
    @Override
    public ResponseResult submitNews(@RequestBody WmNewsDto wmNews) {
            //提交文章
            return wmNewsService.saveNews(wmNews);
    }
    @GetMapping("/one/{id}")
    @Override
    public ResponseResult findWmNewsById(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsById(id);
    }

    @GetMapping("/ss")
    public String ss() {
        return "sss";
    }

    @GetMapping("/del_news/{id}")
    @Override
    public ResponseResult delNews(@PathVariable("id") Integer id) {
        return wmNewsService.delNews(id);
    }

    @PostMapping("/down_or_up")
    @Override
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }

    @GetMapping("/findOne/{id}")
    @Override
    public WmNews findById(@PathVariable("id") Integer id) {
        if (id==null)return null;
        return wmNewsService.getById(id);
    }

    @PostMapping("/update")
    @Override
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        if (wmNews==null ||wmNews.getId()==null)return wmNewsService.errorParam;
        wmNewsService.updateById(wmNews);
        return wmNewsService.succes;
    }

    @GetMapping("/getUserById")
    public WmUser getUserById(@PathVariable Integer wmUserId) {
     if (wmUserId==null)return null;
        WmUser wmUser = wmUserMapper.selectById(wmUserId);
        return wmUser;
    }
}
